<template>
  <el-form style="max-width: 600px;margin-top:10px;" :model="createForm" :rules="rules" label-width="auto"
    class="form_style" :size="formSize" status-icon>
    <h2>添加货品
      <span>请在下面如实填写店铺信息，其中星号为必填项</span>
    </h2>
    <el-form-item prop="productCategoryId" label="所属品类：">
      <el-tree-select v-model="createForm.productCategoryId" :data="productCategoryOptions"
        @change="onCategoryChange" />
    </el-form-item>
    <el-form-item prop="name" label="货品名称：">
      <el-input v-model="createForm.name" />
    </el-form-item>
    <el-form-item prop="price" label="价格：">
      <el-input type="number" v-model="createForm.price" />
    </el-form-item>
    <el-form-item prop="unit" label="单位：">
      <el-input v-model="createForm.unit" />
    </el-form-item>
    <el-form-item prop="priceRules" label="货品价格规则：">
      <el-input v-model="createForm.priceRules" />
    </el-form-item>
    <el-form-item prop="note" label="备注信息：">
      <el-input v-model="createForm.note" />
    </el-form-item>
  </el-form>
  <el-button type="primary" style="margin-left: 50%;" @click="confirm()">提交</el-button>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import { getProductCategory, updateProd, deleteProduct, createProd } from '../../../api';

const createForm = ref({
  keywords: '',
  name: '',
  note: '',
  price: null,
  priceRules: '',
  productCategoryId: null,
  productCategoryName: '',
  sort: 0,
  unit: '',
  unitWeight: 0
})


let productCategoryOptions = ref([])

const buildCategoryTree = (categories) => {
  return categories.map(item => {
    let category = {
      value: item.id,
      label: item.name
    };

    // 如果有子分类，递归处理
    // if (item.children && item.children.length) {
    //   category.children = buildCategoryTree(item.children);
    // }

    return category;
  });
};

const getProductCategoryList = async () => {
  let res = await getProductCategory();
  console.log('商品分类', res.data);
  productCategoryOptions.value = buildCategoryTree(res.data);
  console.log('productCategoryOptions.value是一级菜单的数据', productCategoryOptions.value);
};


const onCategoryChange = (value) => {
  const selectedCategory = findCategoryById(value, productCategoryOptions.value);

  console.log('selectedCategory是当前选中的分类名称', selectedCategory);
  if (selectedCategory) {
    createForm.value.productCategoryName = selectedCategory.label;
    console.log('createForm.value.productCategoryName 是当前选中的分类名字', createForm.value.productCategoryName );
  }
};

const findCategoryById = (id, categories) => {
  for (let category of categories) {
    if (category.value === id) {
      return category;
    } else if (category.children && category.children.length > 0) {
      const found = findCategoryById(id, category.children);
      if (found) return found;
    }
  }
  return null;
};

getProductCategoryList();

const confirm = async () => {
  console.log('更新内容', createForm)
  console.log('更新内容', createForm.value)
  let response = createForm.value
  //把updateForm.value给列表的第一列
  console.log('更新内容', response)
  let res = await createProd(createForm.value);
  console.log('查看一下', createForm)
  console.log('查看一下2', res)
  if (res.code == 200) {
    ElMessage({
      message: '添加成功',
      type: 'success',
      duration: 1000
    });
    getList()
  } else {
    ElMessage({
      message: res.message,
      type: 'error',
      duration: 1000
    });
  }
  dialogFormVisible.value = false
}

const rules = reactive<FormRules<RuleForm>>({
  product_category_id: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  unit: [
    {
      //true添加星号
      required: true,
      message: 'Please select Activity zone',
      trigger: 'change',
    },
  ],
  name: [
    {
      required: true,
      message: 'Please select Activity count',
      trigger: 'change',
    },
  ],
  note: [
    {
      type: 'date',
      // required: true,
      message: 'Please pick a date',
      trigger: 'change',
    },
  ],
  keywords: [
    {
      required: true,
      message: 'Please pick a time',
      trigger: 'change',
    },
  ],
})


const submitForm = async () => {
  const res = await createProd(createForm.value);
}




</script>
<style scoped>
/* 表单的样式 */
.form_style {
  margin-left: auto;
  margin-right: auto;
  max-width: 500px;
  background: #FFF;
  padding: 30px 30px 20px 30px;
  box-shadow: rgba(187, 187, 187, 1) 0 0px 20px -1px;
  -webkit-box-shadow: rgba(187, 187, 187, 1) 0 0px 20px -1px;
  font: 12px Arial, Helvetica, sans-serif;
  color: #666;
  border-radius: 10px;
  -webkit-border-radius: 10p
}

/*大标题样式*/
.form_style h2 {
  font: 24px "Trebuchet MS", Arial, Helvetica, sans-serif;
  padding: 0px 0px 10px 40px;
  display: block;
  border-bottom: 1px solid #F5F5F5;
  margin: -10px -30px 10px -30px;
  color: #354552;
}

/*小标题样式*/
.form_style h2>span {
  display: block;
  font-size: 12px;
}
</style>